﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;

namespace Utils.Entity
{
    public class UserEntity
    {
        private int userID;

        public int UserID
        {
            get { return userID; }
            set { userID = value; }
        }
        private string userName;

        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }
        private string email;

        public string Email
        {
            get { return email; }
            set { email = value; }
        }

        private byte userType;
        public byte UserType
        {
            get
            {
                return userType;
            }
        }

        private string ip;
        public string IP
        {
            get { return ip; }
            set { ip = value; }
        }

        private DateTime? expireDate;
        public DateTime? ExpireDate
        {
            get { return expireDate; }
            set { expireDate = value; }
        }

        private decimal money;
        public decimal Money
        {
            get { return money; }
            set { money = value; }
        }

        private string fbId;
        public string FbId
        {
            get { return fbId; }
            set { fbId = value; }
        }

        private string mobile;
        public string Mobile
        {
            get { return mobile; }
            set { mobile = value; }
        }

        public ArrayList RoleList;

        public UserEntity(int userId, string userName)
        {
            this.userID = userId;
            this.UserName = userName;
        }

        public UserEntity(DataSet userInformation)
        {
            if (userInformation != null && userInformation.Tables.Count == 2)
            {
                //Lay ve thong tin cua nguoi dung
                DataRow row = userInformation.Tables[0].Rows[0];

                userID = Convert.ToInt32(row["UserID"]);
                userName = row["UserName"].ToString().Trim();
                email = row["Email"].ToString().Trim();
                mobile = row["Mobile"].ToString().Trim();
                try
                {
                    money = Convert.ToDecimal(row["Money"]);      
                }
                catch (Exception e)
                {
                }
                userType = Convert.ToByte(row["UserType"]);
                try
                {
                    expireDate = Convert.ToDateTime(row["ExpireDate"]);
                }
                catch (Exception e)
                {
                    expireDate = null;
                }

                //Lay ve danh sach quyen cua nguoi dung
                DataTable dt = userInformation.Tables[1];
                if (dt != null && dt.Rows.Count > 0)
                {
                    IEnumerator ie = dt.Rows.GetEnumerator();
                    UserRoleEntity roleEntity = null;
                    while (ie.MoveNext())
                    {
                        row = (DataRow)ie.Current;
                        roleEntity = new UserRoleEntity();
                        roleEntity.RoleID = Convert.ToInt32(row["RoleID"]);
                        roleEntity.RoleName = row["RoleName"].ToString();
                        roleEntity.UserID = Convert.ToInt32(row["UserID"]);
                        roleEntity.PageID = Convert.ToInt32(row["FunctionPageID"]);
                        if (RoleList == null) RoleList = new ArrayList();
                        RoleList.Add(roleEntity);
                    }
                }
            }
        }
    }
}
